<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}分站`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="substationInfo"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="分站名称" prop="substationName">
        <el-input v-model="substationInfo.substationName" placeholder="请填写分站名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="分站库房号" prop="subWarehouseId">
        <el-input v-model="substationInfo.subWarehouseId" placeholder="请选择分站库房" readonly></el-input>
      </el-form-item>
      <el-form-item label="省" prop="substationAddress">
        <el-input v-model="substationInfo!.substationAddress!.province" placeholder="省" readonly></el-input>
      </el-form-item>
      <el-form-item label="市" prop="substationAddress">
        <el-input v-model="substationInfo!.substationAddress!.city" placeholder="市" readonly></el-input>
      </el-form-item>
      <el-form-item label="区" prop="substationAddress">
        <el-input v-model="substationInfo!.substationAddress!.zone" placeholder="区" readonly></el-input>
      </el-form-item>
      <el-form-item label="详细地址" prop="substationAddress">
        <el-input v-model="substationInfo!.substationAddress!.detail" placeholder="详细地址" readonly></el-input>
      </el-form-item>
      <el-button type="primary" @click="openDepartmentDialog">选择分站库房</el-button>
      <el-button @click="clearDepartment">清空库房信息</el-button>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button @click="resetForm(ruleFormRef)">重置</el-button>
      <el-button type="primary" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
  <WarehouseDialog ref="warehouseRef" @get-warehouse="getWarehouse" />
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Substation, Warehouse } from "@/api/interface";
import WarehouseDialog from "./WarehouseDialog.vue";

interface DrawerProps {
  title: string;
  isView: boolean;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: ""
});
// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

const substationInfo = ref<Partial<Substation.SubstationInfo>>({
  substationAddress: { province: "", city: "", zone: "", detail: "" }
});

const rules = reactive({
  substationName: [{ required: true, message: "请填写分站名称" }],
  subWarehouseId: [{ required: true, message: "请选择分站库房" }]
});
const warehouseRef = ref<InstanceType<typeof WarehouseDialog> | null>(null);
const openDepartmentDialog = () => {
  //分站库房dialog
  warehouseRef.value?.open(1);
};
const clearDepartment = () => {
  substationInfo.value.subWarehouseId = undefined; // 清空departmentId
  substationInfo.value.substationAddress = { province: "", city: "", zone: "", detail: "" }; // 清空departmentName
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    substationInfo.value.creatorId = 1;
    if (!valid) return;

    // if (userInfo.value.departmentId == undefined || userInfo.value.departmentName == undefined) {
    //   ElMessage.error({ message: `请选择部门！` });
    //   return;
    // }
    try {
      await drawerProps.value.api!(substationInfo.value);
      ElMessage.success({ message: `${drawerProps.value.title}分站成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

//获取子组件传来的库房信息
const getWarehouse = (row: Warehouse.ResWarehouseList) => {
  substationInfo.value.subWarehouseId = row.warehouseId;
  substationInfo.value.substationAddress!.province = row.warehouseAddress.province;
  substationInfo.value.substationAddress!.city = row.warehouseAddress.city;
  substationInfo.value.substationAddress!.zone = row.warehouseAddress.zone;
  substationInfo.value.substationAddress!.detail = row.warehouseAddress.detail;
};
defineExpose({
  acceptParams
});
</script>
